博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义分页
阅读量:5140 次
发布时间:2019-06-13

本文共 8133 字,大约阅读时间需要 27 分钟。

一:封装保存

1.1:page.py

# 自定义分页# 带首页和尾页# 官方推荐,页码数为奇数class PageNation:    def __init__(self,base_url,current_page_num,total_counts,request,per_page_counts=10,page_number=5,):        '''        :param base_url:   分页展示信息的基础路径        :param current_page_num:  当前页页码        :param total_counts:  总的数据量        :param per_page_counts:  每页展示的数据量        :param page_number:  显示页码数        # 关键字参数写在最后面,如果有传进来的参数就用 传进来的,如果没有则用默认的        '''        self.base_url = base_url        self.current_page_num = current_page_num        self.total_counts = total_counts        self.per_page_counts = per_page_counts        self.page_number = page_number        self.request = request        try:            self.current_page_num = int(self.current_page_num)        except Exception:            self.current_page_num = 1        if self.current_page_num < 1:            self.current_page_num = 1            # 做一个异常处理, 如果是非字母或者负数 跳转到第一页        half_page_range = self.page_number // 2        # 计算总页数        self.page_number_count, a = divmod(self.total_counts, self.per_page_counts)        if a:            self.page_number_count += 1        if self.current_page_num > self.page_number_count:            self.current_page_num = self.page_number_count        if self.page_number_count <= self.page_number:            self.page_start = 1            self.page_end = self.page_number_count        else:            if self.current_page_num <= half_page_range:  #2                self.page_start = 1                self.page_end = page_number  #5            elif self.current_page_num + half_page_range >= self.page_number_count:                self.page_start = self.page_number_count - self.page_number + 1                self.page_end = self.page_number_count            else:                self.page_start = self.current_page_num - half_page_range                self.page_end = self.current_page_num + half_page_range        import copy        from django.http.request import QueryDict        self.params = copy.deepcopy(request.GET)        # ?condition = qq & wd = 1 & page = 3        # params['page'] = current_page_num        # query_str = params.urlencode()    #数据切片依据,起始位置    @property    def start_num(self):        if self.current_page_num == 0:            self.current_page_num = 1        start_num = (self.current_page_num - 1) * self.per_page_counts        return start_num    #数据切片依据,终止位置    @property    def end_num(self):        end_num = self.current_page_num * self.per_page_counts        return end_num    # 拼接HTMl标签    def page_html(self):        tab_html = ''        tab_html += '
' return tab_html

1.2:views.py

# CBV 写法class CustomerView(View):    def get(selef, request):        all_customers = models.Customer.objects.all() # 获取数据总数量        current_page_num = request.GET.get('page', 1)  # 当前页                per_page_counts = 9  # 每页显示9条        page_number = 9  # 总共显示9个页码               total_count = all_customers.count() # 得到页数总的数据量               #__init__(self,base_url,current_page_num,total_counts,request,per_page_counts=10,page_number=5,)# 关键字参数写在最后面,如果有传进来的参数就用 传进来的,如果没有则用默认的# 按照page里 参数顺序 进行传参        page_obj = page.PageNation(request.path, current_page_num, total_count, request, per_page_counts, page_number)        # 需要传个 request,要不然会报错        all_customers = all_customers.order_by('-pk')[page_obj.start_num:page_obj.end_num]        # 调用page里的start和end方法, page_obj.start_num:page_obj.end_num        # -pk是数据进行倒序排列        ret_html = page_obj.page_html()        # 调用page里的拼接标签        return render(request,'customers.html',{'all_customers':all_customers,'ret_html':ret_html})        # 返回给html页面,数据,和拼接的html

二:函数版本

#函数low版def pagenation(base_url,current_page_num,total_counts,per_page_counts=10,page_number=5):    '''    total_counts    数据总数    per_page_counts   每页分多少条数据    page_number = 页码显示多少个    current_page_num 当前页    :return:    '''    # all_objs_list = models.Customer.objects.all()    # total_counts = all_objs_list.count()    # per_page_counts = 10    # page_number = 5    try:        current_page_num = int(current_page_num)    except Exception:        current_page_num = 1    half_page_range = page_number//2    #计算总页数      # 数据总数  每页分多少条数据    page_number_count,a = divmod(total_counts,per_page_counts)    if current_page_num < 1:        current_page_num = 1    if a:        page_number_count += 1    if current_page_num > page_number_count:        current_page_num = page_number_count    start_num = (current_page_num - 1) * 10    end_num = current_page_num * 10    if page_number_count <= page_number:        page_start = 1        page_end = page_number_count    else:        if current_page_num <= half_page_range:            page_start = 1            page_end = page_number        elif current_page_num + half_page_range  >= page_number_count:            page_start = page_number_count - page_number + 1            page_end = page_number_count        else:            page_start = current_page_num - half_page_range            page_end = current_page_num + half_page_range    #拼接HTMl标签    tab_html = ''    tab_html += '
' return tab_html,start_num,end_num

三:批量添加数据

一般测试分页都需要大量的数据 ,下面一次性的批量添加一些数据在项目里创建一个py文件,名字随便起,但是需要注意: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "crm.settings") 该配置必须是当前项目 manage.py的配置,如果不是则会报错建议直接复制粘贴过来这个配置,避免额外的错误右键执行,这样就批量创建好了数据,最后查看表里有没有创建成功数据
import osif __name__ == "__main__":    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "crm.settings")    import django    django.setup()    from app01 import models    import random    l1=[]    for i in range(1,101):        obj=models.Customer(            qq=''.join([str(i) for i in random.choices(range(1,10),k=11) ]),            name='xiaohei'+str(i),            sex=random.choice(['male','female']),            source=random.choice(['qq','referral','website']),            course=random.choice(['linuxL','PythonFullStack']),        )        l1.append(obj)    models.Customer.objects.bulk_create(l1)

转载于:https://www.cnblogs.com/Quantum-World/p/11024213.html

你可能感兴趣的文章
PE知识复习之PE的导入表
查看>>
HDU 2076 夹角有多大(题目已修改,注意读题)
查看>>
洛谷P3676 小清新数据结构题(动态点分治)
查看>>
九校联考-DL24凉心模拟Day2T1 锻造(forging)
查看>>
洛谷 P3237 [HNOI2014]米特运输
查看>>
Attributes.Add用途与用法
查看>>
JavaScript面向对象初探——封装和继承
查看>>
L2-001 紧急救援 (dijkstra+dfs回溯路径)
查看>>
javascript 无限分类
查看>>
spring IOC装配Bean(注解方式)
查看>>
[面试算法题]有序列表删除节点-leetcode学习之旅(4)
查看>>
SpringBoot系列五:SpringBoot错误处理(数据验证、处理错误页、全局异常)
查看>>
kubernetes_book
查看>>
OpenFire 的安装和配置
查看>>
ZJOI2018游记Round1
查看>>
侧边栏广告和回到顶部
查看>>
https://blog.csdn.net/u012106306/article/details/80760744
查看>>
ios应用版本号设置规则
查看>>
海上孤独的帆
查看>>
error: more than one device and emulator 问题解决
查看>>